package cn.ex.basic.util;

import cn.ex.org.vo.EmailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Slf4j  //@Slf4j是用作日志输出
public class MailUtil {
    //获取bean
    private static JavaMailSender mailSender = BeanUtil.getBean(JavaMailSender.class);

    /**
     * 发送简单邮件信息
     */
    public static void sendSimpleMail(SimpleMailMessage message) {
        mailSender.send(message);
    }


    /**
     * 发送复杂邮件
     */
    public static void sendMessageMail(EmailVo emailVo) {
        try {
            BeanUtil.createBean(mailSender);
            MimeMessage message = mailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(message,true,"utf-8");
            helper.setFrom("2290517709@qq.com");
            helper.setSubject(emailVo.getSubject());
            //第二个参数 true 代表是否是一个html内容
            helper.setText(emailVo.getContent());
            helper.setTo(emailVo.getEmail());
            //发送邮件
            mailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}